package com.onpositive.datamap.values.impl;

import com.onpositive.data.diskstorage.AbstractValueAccessor;
import com.onpositive.data.units.DateWithAccuracy;
import com.onpositive.datamap.IDataPropertyValue;
import com.onpositive.datamap.IDateRange;
import com.onpositive.datamap.IScalarRange;
import com.onpositive.datamap.core.IDataProperty;

public class DateRange extends ScalarValue implements
		IScalarRange<DateWithAccuracy>,IDateRange {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DateWithAccuracy low;
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((high == null) ? 0 : high.hashCode());
		result = prime * result + ((low == null) ? 0 : low.hashCode());
		return result;
	}
	public boolean isNever(){
		return high.isNever();
	}
	protected DateRange(){
		
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		DateRange other = (DateRange) obj;
		if (high == null) {
			if (other.high != null)
				return false;
		} else if (!high.equals(other.high))
			return false;
		if (low == null) {
			if (other.low != null)
				return false;
		} else if (!low.equals(other.low))
			return false;
		return true;
	}

	private DateWithAccuracy high;

	public DateRange(IDataProperty prop, Object comment, DateWithAccuracy low,
			DateWithAccuracy high) {
		super(prop, comment);
		this.low = low;
		this.high = high;
		if (low==null&&high==null){
			throw new IllegalArgumentException();
		}
	}

	public DateRange(IDataProperty prop, Object comment, DateWithAccuracy low) {
		super(prop, comment);
		this.low = low;
		this.high = low;
		if (low==null&&high==null){
			throw new IllegalArgumentException();
		}
	}

	@Override
	public int compareTo(IScalarRange<DateWithAccuracy> o) {
		if (o==null){
			return -1;
		}
		return this.high.compareTo(o.getHigh());
	}

	@Override
	public DateWithAccuracy getLow() {
		if (low==null){
			return getHigh();
					
		}
		return low;
	}

	@Override
	public DateWithAccuracy getHigh() {
		return high;
	}

	@Override
	public String toString() {
		if (high==null){
			return low.toString();
		}
		if (low==null){
			return high.toString();
		}
		if (!low.equals(high)) {
			return low.toString() + "-" + high.toString();
		}
		return low.toString();
	}
	@Override
	public IDataPropertyValue clone(AbstractValueAccessor accessor) {
		IDataProperty clone = this.getProperty().clone(accessor);
		return new DateRange(clone, getComment(), low,high);
	}

	
}
